<!DOCTYPE html>
<html>
<head>
	<title>WebAssembly Thread</title>
</head>
<body>
<script>
	// Standard: https://github.com/WebAssembly/threads/blob/master/proposals/threads/Overview.md
	fetch('main.wasm').then(response => response.arrayBuffer()).then(moduleBuffer => {
		let memory = new WebAssembly.Memory({initial: 1, maximum: 1, shared: true});
		let worker = new Worker('worker.js');
		const mutexAddr = 0;
		worker.postMessage({
			memory,
			moduleBuffer
		});
		
		let imports = {env: {memory: memory}};
		let module = WebAssembly.instantiate(moduleBuffer, imports).then(
	    ({instance}) => {
	        if (instance.exports.tryLockMutex(mutexAddr)) {
				console.log("main thread!");
	            instance.exports.unlockMutex(mutexAddr);
	        }
	    });
	});  
</script>

</body>
</html>

